package day05;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Author：毕龙
 * Date: 2022/11/29 14:36
 */
public class TestYang {
    public static void main(String[] args) {
        //创建JFrame
        JFrame frame = new JFrame("养了个羊");
       //创建面板
        JPanel panel = new JPanel();
        //在面板上设置背景图
        JLabel background= new JLabel(new ImageIcon("images/背景.jpg"));
        //设置背景的大小
        background.setSize(480, 800);
        //设置背景的相对位置
        background.setLocation(0, 0);
        //设置面板的
        panel.setLayout(null);
        panel.add(background);

        //创建集合当所有的按钮
        ArrayList<JButton> buttons = new ArrayList<>();

        //创建一副牌
        ArrayList<JButton> cards = createCards(6);
        print(cards);
        //打乱牌
        Collections.shuffle(cards);
        print(cards);


        frame.add(panel);


        frame.setSize(495, 800);
        //设置窗体的相对位置
        frame.setLocationRelativeTo(null);
        //设置窗体的关闭方式
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗体是否显示
        frame.setVisible(true);
    }

    /**
     * 打印全部的牌
     */
    public static void print(ArrayList<JButton> cards){
        for (int i = 0; i < cards.size(); i++) {
            JButton card = cards.get(i);
            System.out.print(card.getName()+"  ");
            if((i+1)%6 == 0){
                System.out.println();
            }
        }
        System.out.println();
    }

    /**
     * 创建牌
     * 定义一个方法封装牌的创建过程 返回值是创建好的全部牌
     * round 参数代表轮次 本案例中需要使用3的倍数
     */
    //创建一副牌
    public static ArrayList<JButton> createCards(int round){
        String[] name = {"刷子","剪刀","叉子","奶瓶","干草","手套","树桩",
                "棉花","毛线","水桶","火","玉米","白菜","草","萝卜","铃铛"};

        ArrayList<JButton> cards = new ArrayList<>();

        for (int i = 0; i < round; i++) {
            for (int j = 0; j < name.length; j++) {
                String fileName = "images/"+name[i]+".png";
                String fileName2 = "images/"+name[i]+"2.png";
                JButton card = new JButton(new ImageIcon(fileName));
                card.setDisabledIcon(new ImageIcon(fileName2));
                card.setSize(59,66);
                card.setName(name[i]);
                cards.add(card);
            }

        }
        return cards;

    }


}
